package com.lanyou.esb.web.serviceTesting;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import redis.clients.jedis.exceptions.JedisException;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.service.UserLogService;
import com.lanyou.cook.service.security.UserSessionService;
import com.lanyou.esb.service.ServiceUsabilityService;
import com.lanyou.esb.vo.HttpVo;
import com.lanyou.esb.web.param.HttpInterfaceParam;

/**
 * 服务可用性检测
 * 
 * @author Davey.wu
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/serviceUsability")
public class ServiceUsabilityController {
	@Autowired
	ServiceUsabilityService serviceUsabilityService;
	@Autowired
	UserSessionService userSessionService;

	@Autowired
	private UserLogService userLogService;

	/**
	 * 获取服务可用性检测结果
	 * 
	 * @param param
	 *            ：查询条件
	 * @param pageable
	 *            ：分页对象
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getServiceUsabilityTestResult")
	public Page<HttpVo> getServiceUsabilityTestResult(HttpInterfaceParam param,
			Pageable pageable) throws Exception {
		// 获取当前用户可访问的系统标识
		Collection<String> collection = userSessionService
				.findFieldSystemIdsOfCurrentAuthenticatedUserCanManage();
		List<String> list = null;
		if (collection != null && collection.size() > 0) {
			list = Arrays.asList(collection.toArray(new String[0]));
		}
		Page<HttpVo> page = null;
		// 获取数据
		try {
			page = serviceUsabilityService.getCheckResult(param,
					list, pageable);
		} catch (Exception e) {
			if (e instanceof JedisException) {
				throw new Exception("连接不上Redis服务器！");
			}
			throw e;
		}
		return page;
	}

}
